<template>
  <div class="dropdownTrigger">
    <!-- 底板 -->
    <div class="dtfloor" @click="panelBool_change" v-if="panelBool"></div>
    <!-- 当前选中项 -->
    <div class="activeItemCont no_copy" @click="panelBool_change">
      <template v-if="activeItem&&activeItem.eventId">
        <i :class="activeItem.icon"></i>
        <span>{{ activeItem.eventName }}</span>
      </template>
      <template v-else>
        <i></i>
        <span>暂无指标</span>
      </template>
    </div>
    <!-- <template v-if="activeItem&&activeItem.eventId">
      <el-button @click="panelBool_change">
        {{ activeItem.eventName }}
      </el-button>
    </template>
    <template v-else>
      <el-button @click="panelBool_change">
        暂无指标
      </el-button>
    </template> -->
    <!-- 展板显示区域 -->
    <!-- :style="{height:panelBool?'5rem':'0'}" -->
    <div
      class="panel no_copy"
      v-if="overallScreeningDataCopy&&overallScreeningDataCopy.length>0"
      :style="{height:panelBool?'5rem':'0',overflow:show_model}"
    >
      <div class="left_p">
        <div
          @click="jumpAnchor(_i)"
          :class="
            dq_par == _i.id
              ? 'anchorPoint anchorPoint_active flex_btwv'
              : 'anchorPoint flex_btw'
          "
          class="anchorPoint flex_btw"
          v-for="_i in overallScreeningDataCopy"
          :key="_i.id"
        >
          <span></span>
          <span>{{ _i.groupName }}</span>
          <span v-if="_i.mateEventList">{{ _i.mateEventList.length }}</span>
        </div>
      </div>
      <div class="right_p">
        <header class="flex_sta">
          <el-input v-model="search_query" placeholder="请输入内容" size="mini" class="right_top_search">
            <el-button @click="filter_arr" slot="append" icon="el-icon-setting"></el-button>
          </el-input>
        </header>
        <section id="dropdownTriggerscrollsection">
          <div
            class="sec_cont"
            :id="'dropdownTrigger'+_i.id"
            v-for="_i in mappingArray"
            :key="_i.id"
          >
            <div class="sec_con_til flex_sta">
              {{_i.groupName}}
              <i></i>
            </div>
            <div class="sce_con_con">
              <span
                :class="activeItem&&activeItem.id&&activeItem.id==_is.id?'active_item':''"
                @click="activeItem = _is;panelBool=false"
                v-for="_is in _i.mateEventList"
                :key="_is.id"
              >
                {{_is.eventName}}
                <div class="more">
                  <div v-if="_is.anme">{{_is.eventName}}</div>
                  <div v-if="_is.propTxt">{{_is.propTxt}}</div>
                  <div v-if="_is.ps">{{_is.ps}}</div>
                </div>
              </span>
            </div>
          </div>
        </section>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "dropdownTrigger",
  props: {
    //  接受的进行处理的全部数据
    overallScreeningData: {
      type: Array,
      default: () => {
        return [];
      }
    },
    //  接受的当前选中项
    activeItem: {
      type: Object,
      default: () => {
        return {};
      }
    }
  },
  watch: {
    panelBool() {
      if (this.panelBool) {
        setTimeout(() => {
          if (this.panelBool) {
            this.show_model = "visible";
          }
        }, 600);
      } else {
        this.show_model = "hidden";
        this.search_query = "";
      }
    },
    activeItem(newV) {
      this.$emit("change", newV);
    }
  },
  components: {},
  data() {
    return {
      antiShake: false,
      show_model: "hidden",
      search_query: "",
      dq_par: null,
      search: null, // 搜索关键字
      panelBool: false,
      overallScreeningDataCopy: []
    };
  },
  methods: {
    panelBool_change() {
      if (this.antiShake) {
        return;
      }
      this.panelBool = !this.panelBool;
      this.antiShake = true;
      setTimeout(() => {
        this.antiShake = false;
      }, 600);
    },
    filter_arr() {
      this.panelBool_change();
      this.$emit("groupChange");
    },
    jumpAnchor(obj) {
      this.dq_par = obj.id;
      this.search_query = "";
      try {
        this.$el.querySelector("#dropdownTrigger" + obj.id).scrollIntoView({
          behavior: "smooth", // 平滑过渡
          block: "nearest" // 上边框与视窗顶部平齐
        });
      } catch (err) {
        console.log(err, "定位错误");
      }
    }
  },
  created() {
    // 初始化当前选中项
    try {
      let _arr = JSON.parse(JSON.stringify(this.overallScreeningData));
      let _copy = [];
      _arr.map(_i => {
        _copy.push(JSON.parse(JSON.stringify(_i)));
      });
      this.overallScreeningDataCopy = _copy;
    } catch (err) {
      console.log(err, "错误22");
    }
  },
  computed: {
    //   映射数组，根据全部数据和查询关键字筛选显示数组
    mappingArray() {
      let str = this.search_query.toLowerCase();
      let arr = [];
      try {
        this.overallScreeningDataCopy.map((_i, _in) => {
          arr[_in] = {
            appId: _i.appId,
            appName: _i.appName,
            bizKey: _i.bizKey,
            extInfo: _i.extInfo,
            groupName: _i.groupName,
            icon: _i.icon,
            id: _i.id,
            mateEventList: []
          };
          _i.mateEventList.map(_is => {
            let str_s = _is.eventName ? _is.eventName.toLowerCase() : "";
            if (str_s.indexOf(str) !== -1) {
              arr[_in].mateEventList.push({
                eventId: _is.eventId,
                eventName: _is.eventName
              });
            }
          });
        });
        for (let index = 0; index <= arr.length - 1; index++) {
          if (arr[index].mateEventList.length == 0) {
            arr.splice(index, 1);
            --index;
          }
        }
      } catch (err) {
        console.log(err, "错误33");
      }
      return arr;
    }
  }
};
</script>

<style scoped lang="scss">
* {
  box-sizing: border-box;
}
.dropdownTrigger {
  // overflow:visible;
  position: relative;
  // max-width: 2.5rem;
  display: inline-block;
  height: 0.52rem;
  height: 32px !important;
  border: 1px solid rgba($color: #dcdfe6, $alpha: 1);
  border-radius: 3px;
  margin: 0 0.1rem;
  background: white;
  transition: all ease 0.5s;
  .activeItemCont {
    // height: 0.46rem;
    display: inline-block;
    // min-width: 1rem;
    // line-height: 0.4rem;
    font-size: 13px;
    margin: 0 0.2rem;
    line-height: 0.52rem;
    height: 0.52rem;
    i {
      margin: 0 0.1rem 0 0.2rem;
    }
    span {
      margin: 0 0.2rem 0 0;
      font-size: 0.1rem;
      letter-spacing: 0.01rem;
    }
  }
  .panel {
    .right_p {
      position: absolute;
      right: 0;
      top: 0.1rem;
      bottom: 0.1rem;
      width: calc(7rem - 1px);
      header {
        width: 99%;
        height: 0.46rem;
        margin: 0 auto 0.1rem;
        // border-bottom: 1px solid rgba($color: #dcdfe6, $alpha: 0.8);
      }
      section::-webkit-scrollbar {
        display: none;
      }
      section {
        width: 99%;
        height: 4.1rem;
        margin: 0 auto 0.1rem;
        overflow-y: scroll;
        scrollbar-width: none; /*Firefox*/
        -ms-overflow-style: none;
        .sec_cont {
          width: 100%;
          margin: 0 auto 0.2rem;
          .sec_con_til {
            width: 100%;
            height: 0.64rem;
            line-height: 0.64rem;
            font-size: 13px;
            font-weight: 500;
            text-indent: 0.2rem;
            color: #475669;
            position: relative;
            i {
              position: absolute;
              display: inline-block;
              background: rgba($color: #01b27a, $alpha: 0.8);
              width: 0.04rem;
              height: 40%;
              top: 30%;
              left: 0.1rem;
            }
          }
          .sce_con_con {
            width: 98%;
            margin: 0 auto;
            span {
              width: calc(50% - 0.2rem);
              height: 0.5rem;
              margin: 0 0.1rem;
              line-height: 0.5rem;
              text-align: left;
              text-indent: 0.2rem;
              display: inline-block;
              overflow: hidden;
              text-overflow: ellipsis;
              white-space: nowrap;
              font-size: 0.12rem;
              border-radius: 4px;
              .more {
                position: absolute;
                width: 4rem;
                top: 0;
                right: -4.1rem;
                opacity: 0;
                padding: 0.1rem 0.1rem;
                border-radius: 0.1rem;
                transition: all 0.5s ease;
                box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
                border: 1px solid #ebeef5;
                background-color: #fff;
                div:nth-child(1) {
                  width: 100%;
                  height: 0.4rem;
                  font-size: 0.24rem;
                  font-weight: bold;
                  line-height: 0.4rem;
                  overflow: hidden;
                  text-overflow: ellipsis;
                  white-space: nowrap;
                }
                div:nth-child(2) {
                  width: 100%;
                  text-indent: 0.3rem;
                  padding: 0.1rem 0 0.2rem;
                  font-size: 0.1rem;
                  line-height: 0.3rem;
                }
                div:nth-child(3) {
                  width: 100%;
                  height: 0.4rem;
                  font-size: 0.1rem;
                  border-top: 1px solid rgba($color: #e4e7ed, $alpha: 0.8);
                  line-height: 0.4rem;
                }
              }
            }
            span:hover {
              background: rgba($color: #e4e7ed, $alpha: 0.4);
              .more {
                opacity: 1;
              }
            }
            .active_item {
              color: white;
              font-weight: 500;
              background: rgba($color: #01b27a, $alpha: 0.6) !important;
            }
          }
        }
      }
    }
    position: absolute;
    left: 0;
    top: 0.5rem;
    width: 9rem;
    height: 5rem;
    z-index: 12;
    transition: all ease 0.5s;
    border-radius: 6px;
    box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
    // border: 1px solid #ebeef5;
    background-color: #fff;
    .left_p {
      scrollbar-width: none; /*Firefox*/
      -ms-overflow-style: none;
    }
    .left_p::-webkit-scrollbar {
      display: none;
    }
    .left_p {
      position: absolute;
      left: 0;
      top: 0.1rem;
      bottom: 0.1rem;
      width: 2rem;
      overflow-y: scroll;
      border-right: 0.01rem solid rgba($color: #909399, $alpha: 0.2);
      .anchorPoint {
        width: 100%;
        height: 0.6rem;
        margin: 0 0 0.1rem 0;
        span {
          display: inline-block;
          height: 0.6rem;
          line-height: 0.6rem;
        }
        span:nth-child(1) {
          border-left: 0.06rem solid rgba($color: #01b27a, $alpha: 0);
          height: 0.3rem;
        }
        span:nth-child(2) {
          text-indent: 0.2rem;
          overflow: hidden;
          text-overflow: ellipsis;
          white-space: nowrap;
          flex: 1;
          font-size: 0.2rem;
        }
        span:nth-child(3) {
          width: 0.4rem;
          height: 0.3rem;
          transform: scale(0.9, 0.8);
          margin-right: 0.06rem;
          line-height: 0.3rem;
          font-size: 0.1rem;
          text-align: center;
          background: rgba($color: #e4e7ed, $alpha: 0.8);
          border-radius: 0.2rem;
          font-weight: 500;
          color: #909399;
        }
      }
      .anchorPoint:hover {
        background: rgba($color: #e4e7ed, $alpha: 0.3);
      }
      .anchorPoint_active {
        background: rgba($color: #04cb94, $alpha: 0.06) !important;
        span:nth-child(1) {
          border-left: 0.06rem solid rgba($color: #01b27a, $alpha: 0.8);
        }
        span:nth-child(2) {
        }
        span:nth-child(3) {
          font-weight: 500;
          background: rgba($color: #04cb94, $alpha: 0.8);
          color: white;
        }
      }
    }
  }
  .dtfloor {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    z-index: 11;
    background: rgba($color: #01b27a, $alpha: 0);
  }
}
.dropdownTrigger:hover {
  // border: 1px solid rgba($color: #01b27a, $alpha: 1);
  // box-shadow: 0.02rem 0.02rem 0.01rem rgba($color: #01b27a, $alpha: 0.2);
  .activeItemCont {
    color: rgba($color: #01b27a, $alpha: 0.8);
  }
}
</style>
